Tips for DA converter


If your MCU board has DA converter, you can monitor the variables on the oscilloscope by outputing them via DA converter. It is very helpful in debugging your program. In this tip, it is explained how you can change the content of DA converter ( that is, variable to display ) easily in real time.

Step 1 : Modify da.h file

easyDSP supports c source file and its header file (da.c and da.h) for dac control. File da.h is like below.

// File name : da.c
// function : DA output control

// variable explanation(#=1,2,3,4)
// da# : address of variable
// da#_type = 0 ; the variable is float
// = 1 ; the variable is integer
// da#_mid : mid value
// da#_rng : da scale

// use this routine in EasyDSP as below
// da1=&var_float
// da1_type=0
// da1_mid=0.
// da1_rng=20
// da2 = &var_int
// da2_type = 1
// da2_mid= 0.
// da2_rng = 20

#ifndef _DA_EasyDSP
#define _DA_EasyDSP

// you should specify the da address of your own
#define DA1_ADDR (*(int *)0X03C000e)
#define DA2_ADDR (*(int *)0X03C000d)
#define DA3_ADDR (*(int *)0X03C000b)
#define DA4_ADDR (*(int *)0X03C0007)

#define

extern unsigned int da1, da2, da3, da4, da1_type, da2_type, da3_type, da4_type;
extern float da1_rng, da1_val, da1_mid;
extern float da2_rng, da2_val, da2_mid;
extern float da3_rng, da3_val, da3_mid;
extern float da4_rng, da4_val, da4_mid;

// Notice : If you need faster DA output, please replace 'divide' part
// in the macro with 'multiply' accordingly.

// 12 bit DA
#define DA12(num) \
da##num##_val = (da##num##_type == 0 ? *(float *)da##num : (float)(*(int *)da##num)) ; \
DA##num##_ADDR = (int)((da##num##_val-da##num##_mid )* 0x7ff/da##num##_rng) + 0x800 ;

// 8 bit DA
#define DA8(num) \
da##num##_val = (da##num##_type == 0 ? *(float *)da##num : (float)(*(int *)da##num)) ; \
DA##num##_ADDR = (int)((da##num##_val-da##num##_mid )*0x7f/da##num##_rng) + 0x80 ;

#endif

 

At first, the address of da converter on your board should be defined correctly in the DA#_ADDR define lines(#=1,2,3,4). And then, you should also modify the macro function for dac output considering the feature of your dac's own. In above example code, 8 bit and 12bit dac with positive/negative output dac are shown.
Note : divide operation in the macro may need long time to be executed. For faster da output, replace it by the multiply operation.

Necessary variables are defined in the da.c file and their meanings are
da# = The address of variable which is output to DA channel #
da#_type = The type of variable. 1 = Integer, 0 = float
da#_rng = range of display
da#_mid = mid value of display
 

Step 2 : Modify your program

Make your MCU program contain the da.c and da.h you modified. And insert following macros where you want dac output is made .Normally, the insertion place is in the timer interrupt routine for repetitive output.

#include "da.h"

..........

DA12(1);
DA12(2);
DA12(3);
DA12(4);

.......

Step 3 : Use easyDSP

Finally, you can control the da converter in the command window or other windows as follows.

da1=&var_float
da1_type=
0
da1_mid=0
da1_rng=20

da2 = &var_int
da2_type =
1
da2_mid= 0
da2_rng = 20